<?php

namespace Modules\Shop\Http\Controllers;

use HiCommon\Service\AccountSettleService;
use HiCommon\Service\CommonService;
use HiCommon\Service\LoginShopUserService;
use HiCommon\Service\MenuService;
use HiCommon\Service\ShopService;
use Illuminate\Support\Facades\Request;

class IndexController extends BaseController
{

    /**
     * 登录界面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login(){
        return view('shop::index.login');
    }

    /**
     * 登录操作
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login_in(){
        try {
            $arr_input = Request::input();//登录表单信息
            $arr_input['ip'] = (string)Request::ip();//登录ip
            return LoginShopUserService::web_login_in($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    public function main(){
        try {
            //获取菜单
            $arr_shop_user = shop_user()->toarray();
            $n_uid = $arr_shop_user['id'];
            $arr_menu = MenuService::api_menu_shop($n_uid);

            $arr_data = [
              'arr_admin'=>$arr_shop_user,
              'arr_menu_list'=>$arr_menu,
            ];
            //        dd($arr_data);
            return view('shop::index.main',$arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    public function welcome(){
        $arr_data = [];
        $n_shop_id = shop_shop_id();
        $result = AccountSettleService::get_settle_order_info_for_shop($n_shop_id);
        if($result['state'] != 0){
            msg_error($result['msg']);
            return 'ok';
        }
        $arr_data['settle_data'] = $result['data'];
        return view('shop::index.welcome',$arr_data);

    }

    public function finance_graphs(){
        $arr_data = [];
        $n_shop_id = shop_shop_id();
        $s_date_type = Request::input('date_type','month');
        $result = ShopService::api_nearly_order_graphs($n_shop_id,$s_date_type);
        if($result['state'] != 0){
            msg_error($result['msg']);
            return 'ok';
        }
        $arr_data['arr_data'] = json_encode($result['data']);
        $arr_data['s_date_type'] = $s_date_type;

        return view('shop::index.finance_graphs',$arr_data);

    }

    /**
     * 退出登录操作
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login_out(){
        try {
            return LoginShopUserService::web_login_out();
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 发送手机验证码
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function send_phone_code(){
        try {
            $s_phone = str_trim(Request::input('phone',''));//发送验证码手机号
            return CommonService::api_send_phonecode($s_phone);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

}
